AS = nasm
CC = gcc
LD = ld 
RM = rm -f
DD = dd
OBJCOPY = objcopy
BOCHS = 

# if don't use std libs , otherwise there will be error that cannot find std libs
# CFLAGS = -Wall -std=c99 -m32 -nostdlib -nostdinc -ffreestanding -I./includes 
CFLAGS = -Wall -std=c99 -m32 -ffreestanding -I./includes 
LDFLAGS = -T link.ld --warn-common
BOCHSFLAGS =


all: clean floppy.img

run: clean floppy.img
	$(EMU) $(EMUFLAGS) floppy.img

clean:
	$(RM) *.img
	$(RM) boot/*.bin
	$(RM) boot/*.o
	$(RM) core/*.bin
	$(RM) core/*.o

floppy.img: bootsect.bin core.bin
	$(DD) if=/dev/zero of=../images/floppy.img bs=512 count=2880
	$(DD) if=boot/bootsect.bin of=../images/floppy.img conv=notrunc
	$(DD) if=core/core.bin of=../images/floppy.img conv=notrunc seek=1

bootsect.bin:
	$(AS) boot/bootsect.asm -f bin -o boot/bootsect.bin

core.bin:
	$(CC) $(CFLAGS) -c boot/boot.c -o boot/boot.o
	
	$(CC) $(CFLAGS) -c core/main.c -o core/main.o
	$(CC) $(CFLAGS) -c core/sf_memory.c -o core/sf_memory.o
	$(CC) $(CFLAGS) -c core/sf_core_service.c -o core/sf_core_service.o
	$(CC) $(CFLAGS) -c core/sf_bplustree.c -o core/sf_bplustree.o
	$(CC) $(CFLAGS) -c core/sf_interrupt.c -o core/sf_interrupt.o
	$(CC) $(CFLAGS) -c core/sf_memory.c -o core/sf_memory.o
	$(CC) $(CFLAGS) -c core/sf_memory_cache.c -o core/sf_memory_cache.o
	$(CC) $(CFLAGS) -c core/sf_paging.c -o core/sf_paging.o
	$(CC) $(CFLAGS) -c core/sf_proc_manage.c -o core/sf_proc_manage.o
	$(CC) $(CFLAGS) -c core/sf_task_manage.c -o core/sf_task_manage.o
	
	$(CC) $(CFLAGS) -c filesystem/sf_filesystem.c -o filesystem/sf_filesystem.o
	$(CC) $(CFLAGS) -c filesystem/sf_block.c -o filesystem/sf_block.o
	$(CC) $(CFLAGS) -c filesystem/sf_drive.c -o filesystem/sf_drive.o
	$(CC) $(CFLAGS) -c filesystem/sf_file.c -o filesystem/sf_file.o
	$(CC) $(CFLAGS) -c filesystem/sf_node.c -o filesystem/sf_node.o
	$(CC) $(CFLAGS) -c filesystem/sf_file_descriptor.c -o filesystem/sf_file_descriptor.o
	
	$(LD) $(LDFLAGS) -o core/core.o boot/boot.o core/main.o core/sf_memory.o core/sf_core_service.o core/sf_bplustree.o core/sf_interrupt.o core/sf_memory.o core/sf_memory_cache.o core/sf_paging.o core/sf_proc_manage.o core/sf_task_manage.o filesystem/sf_filesystem.o filesystem/sf_block.o filesystem/sf_drive.o filesystem/sf_file.o filesystem/sf_node.o filesystem/sf_file_descriptor.o
	$(OBJCOPY) -I pe-i386 -v -O binary core/core.o core/core.bin